package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 通过手机号码查询会员
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    /**
     * 新增会员
     * @param member
     */
    @Override
    public void add(Member member) {

        memberMapper.add(member);

    }

    /**
     * 统计过去12个月的会员数量
     * @return
     */
    @Override
    public Map getMemberReport() {
        //获取日历对象
        Calendar calendar = Calendar.getInstance();
        //设置日历往前推12个月
        calendar.add(Calendar.MONTH,-12);

        //定义一个list集合，存放过去12个月每个月的月份
        List<String> months = new ArrayList<>();

        //定义一个list集合，存放过去12个月每个月的会员数量
        List<Integer> memberCounts = new ArrayList<>();

        //遍历过去12个月的每一个月
        for (int i = 0; i < 12; i++) {

            //获取每个月的时间
            Date time = calendar.getTime();
            //获取每个月的月份
            String month = new SimpleDateFormat("yyyy-MM").format(time);

            //定义每个月开始日期
            String monthBegin = month+"-1";
            //定义每个月结束时间
            String monthEnd = month+"-31";

            //统计每个月人数
            int count = memberMapper.findMemberCountByMonth(monthBegin,monthEnd);

            //添加每一个月
            months.add(month);

            //添加每一个月的会员数量
            memberCounts.add(count);

            //每次循环都在日历的月份上+1，如-11月,-10月，-9月，依次类推
            calendar.add(Calendar.MONTH,+1);

        }

        //创建一个map，将月份的集合以及每个月会员数量的集合存入其中
        Map<String, List> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCounts",memberCounts);

        return map;
    }

    //查询一定时间段内的每个月会员数量
    @Override
    public Map getMemberReportByDateRange(String startDate,String endDate) {
        Map map = new HashMap<>();
        try {
            //开始月份
            Date monthStart = DateUtils.parseString2Date(startDate, "yyyy-MM");
            //结束月份
            Date monthEnd = DateUtils.parseString2Date(endDate, "yyyy-MM");
            //定义开始日期
            String dateBegin = new SimpleDateFormat("yyyy-MM").format(monthStart) + "-01";
            //定义结束日期
            String dateEnd = new SimpleDateFormat("yyyy-MM").format(monthEnd) + "-31";
            //查询当前时间段内的每个月会员数量
            List<Map> list = memberMapper.getMemberReportByDateRange(dateBegin,dateEnd);
            List<String> months = new ArrayList<>();
            List memberCounts = new ArrayList<>();
            for (Map map1 : list) {
                String month = (String) map1.get("month");
                Long memberCount = (Long) map1.get("memberCount");
                months.add(month);
                memberCounts.add(memberCount.toString());
            }
            System.out.println(months);
            System.out.println(memberCounts);
            map.put("months",months);
            map.put("memberCounts",memberCounts);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map getSexReport() {
        List<Map> getSexCounts = memberMapper.getSexReport();
        /*定义一个存储性别的集合*/
        List<String> getMemberSex= new ArrayList<>();
        for (Map map : getSexCounts) {
            String name = (String) map.get("name");
            getMemberSex.add(name);
        }
        /*将数据分装到map里*/
        Map map = new HashMap<>();
        map.put("getMemberSex", getMemberSex);
        map.put("getSexCounts",getSexCounts);

        return map;

    }

    @Override
    public Map getMemberReportByAge() {
        List<String> names = new ArrayList<>();
        List<Map> counts= memberMapper.getMemberReportByAge();
        for (Map count : counts) {
            String name = (String) count.get("name");
            names.add(name);
        }
        Map map= new HashMap<>();
        map.put("names",names);
        map.put("counts",counts);
        return map;
    }


}
